<?php

namespace app\api\controller;

use app\api\server\Promotion as ServerPromotion;
use app\api\server\User as ServerUser;
use app\common\controller\Api;
use app\common\model\Banner;
use app\common\model\Frozenlog;
use app\common\model\MoneyLog;
use app\common\model\order\Order;
use app\common\model\performance\Log;
use app\common\model\User;
use app\common\model\UserGroup;

/**
 * 推广接口
 */
class Promotion extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @date: 2024-01-05 
     * @ApiTitle (收益统计)
     * @ApiMethod (GET)
     * @ApiMethod (POST)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function index()
    {
        $uid = $this->auth->id;
        $group_id = $this->auth->group_id;
        //今日预估收益
        $todaymoney = Frozenlog::where('user_id', $uid)->whereTime('createtime', 'today')->sum('money');
        //本月预估收益
        $monthmoney = Frozenlog::where('user_id', $uid)->whereTime('createtime', 'month')->sum('money');
        $uids = ServerUser::diguis($uid, 5);
        if ($uids) {
            foreach ($uids as $kk => $vv) {
                $ids = User::where("find_in_set({$vv},paren_ids)")->column('id');
                array_push($ids, $vv);
                $total_fee = Order::whereIn('user_id', $ids)->whereTime('paytime', 'today')->sum('performance');
                // $total_num = Order::whereIn('user_id', $ids)->whereTime('paytime', 'month')->sum('total_num');
                // $total_price = Order::whereIn('user_id', $ids)->whereTime('paytime', 'month')->sum('performance');
                // $total_count = Order::whereIn('user_id', $ids)->whereTime('paytime', 'year')->sum('total_num');
                // if ($total_num >= 300 && $total_price >= 210000) {
                //     $todaymoney += bcmul($total_price, 0.05, 2);
                //     $monthmoney += bcmul($total_price, 0.05, 2);
                // }
                // if ($total_count >= 3000) {
                //     $todaymoney += 50000;
                //     $monthmoney += 50000;
                // }
                if ($group_id == 3) {
                    $todaymoney += bcmul($total_fee, 0.05, 2);
                } elseif ($group_id == 4) {
                    $todaymoney += bcmul($total_fee, 0.03, 2);
                }
            }
        }
        if ($group_id == 5) {
            $team_ids = User::where("find_in_set({$uid},paren_ids)")->column('id');
            $total_money = Order::whereIn('user_id', $team_ids)->whereTime('paytime', 'month')->sum('performance');
            $monthmoney += bcmul($total_money, 0.05, 2);
        }
        //累计收益
        $total_earnings = MoneyLog::where('user_id', $uid)->where('money', '>', 0)->sum('money');
        //粉丝数量
        $nums = User::where("pid", $uid)->column('id');
        $count = count($nums);
        if ($nums) {
            $count += User::whereIn("pid", $nums)->count();
        }
        $this->success('请求成功', compact('todaymoney', 'monthmoney', 'total_earnings', 'count'));
    }
    /**
     * @Author LK
     * @date: 2024-01-05 
     * @ApiTitle (身份)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function group_list()
    {
        $this->success('', UserGroup::field('id,name')->select());
    }
    /**
     * @Author LK
     * @date: 2024-01-05 
     * @ApiTitle (我的粉丝)
     * @ApiMethod (GET)
     * @ApiParams (name="type", type="string", required=true, sample="", description="1=一级粉丝，2=二级粉丝")
     * @ApiParams (name="group_id", type="string", required=true, sample="", description="粉丝身份")
     * @ApiParams (name="class", type="string", required=true, sample="", description="year;month;day")
     * @ApiParams (name="keywords", type="string", required=true, sample="", description="条件")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function my_fans()
    {
        $param = request()->param();
        extract($param);
        $fans = new User;
        $uid = $this->auth->id;
        if (isset($type) && $type) {
            if ($type == 1) {
                $fans->where('pid', $uid);
            } else {
                $fans->where('tid', $uid);
            }
        } else {
            $fans->where('pid|tid', $uid);
        }
        if (isset($group_id) && $group_id) {
            $fans->where('group_id', $group_id);
        }
        $lists = $fans->with(['group'])->paginate();
        if ($class == 'year') {
            $where['createtime'] = ['between', [strtotime($keywords . '-1-1'), strtotime($keywords . '-12-31 23:59:59')]];
        } elseif ($class == 'month') {
            $where['createtime'] = ['between', [strtotime($keywords . '-1'), strtotime($keywords . '-d 23:59:59')]];
        } elseif ($class == 'day') {
            $where['createtime'] = ['between', [strtotime($keywords[0]), strtotime($keywords[1] . ' 23:59:59')]];
        }
        foreach ($lists as $k => $v) {
            $where['user_id'] =  $v->id;
            //累计收益
            $v->earnings = MoneyLog::where($where)->where('money', '>', 0)->sum('money');
            //业绩
            $v->performance = Log::where($where)->where('performance', '>', 0)->sum('performance');
        }
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @date: 2024-01-05 
     * @ApiTitle (佣金明细)
     * @ApiMethod (GET)
     * @ApiParams (name="starttime", type="string", required=true, sample="", description="")
     * @ApiParams (name="endtime", type="string", required=true, sample="", description="")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function commission_detail()
    {
        $param = request()->param();
        extract($param);
        $log = new MoneyLog;
        $uid = $this->auth->id;
        if (isset($starttime) && $starttime && isset($endtime) && $endtime) {
            $log->whereTime('createtime', 'between', [$starttime, $endtime]);
        }
        $lists = $log->where('user_id', $uid)->order('id desc')->paginate();
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @date: 2024-01-05 
     * @ApiTitle (分销订单)
     * @ApiMethod (GET)
     * @ApiParams (name="type", type="string", required=true, sample="", description="1=待返佣,2=已结算")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function distribution_order()
    {
        $param = request()->param();
        extract($param);
        $log = new Frozenlog;
        if ($type == 1) {
            $log->where('frozenlog.is_settle', 0);
        } elseif ($type == 2) {
            $log->where('frozenlog.is_settle', 1);
        }
        $lists =  $log->with(['user'])->where('order_id', '>', 0)->where('user_id', $this->auth->id)->order('id desc')->paginate();
        foreach ($lists as $k => $v) {
            $v->order = Order::get($v->order_id, ['goods']);
        }
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @date: 2024-01-05 
     * @ApiTitle (团队销售业绩)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiParams (name="type", type="string", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function sales_performance()
    {
        $lists = (new ServerPromotion)->team_yj();
        $this->success('', $lists);
    }
}
